package view;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;

import javax.swing.JTable;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import model.Protokoll;

import controller.MainController;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;

public class ProtokolleVerwaltenDialog extends JDialog {

	private MainController mainController;

	private int patientenId;

	private DefaultTableModel modelProtokolle;

	private JTable tableProtokolle;

	/**
	 * Create the dialog.
	 */
	public ProtokolleVerwaltenDialog(JDialog owner, MainController mainController, int patientenId) {
		super(owner, true);
		this.mainController = mainController;
		this.patientenId = patientenId;
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Protokolle verwalten - " + this.mainController.getPatientenController().getPatient(this.patientenId));
		setBounds(100, 100, 600, 400);
		
		JScrollPane scrollPane = new JScrollPane();
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		modelProtokolle = new DefaultTableModel() {
			Class[] columnTypes = new Class[] {Integer.class, java.util.Date.class, String.class};
			
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			
			boolean[] columnEditables = new boolean[] {false, false, false};
			
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		};
		modelProtokolle.addColumn("Termin-Id");
		modelProtokolle.addColumn("Datum");
		modelProtokolle.addColumn("Inhalt");
		
		tableProtokolle = new JTable(modelProtokolle);
		tableProtokolle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableProtokolle.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		DefaultTableCellRenderer renderer = new MyDateRenderer();
		tableProtokolle.setDefaultRenderer(java.util.Date.class, renderer);
		scrollPane.setViewportView(tableProtokolle);
		tableProtokolleAktualisieren();

		if (modelProtokolle.getRowCount() > 0) {
			tableProtokolle.setRowSelectionInterval(0, 0);
		}
		
		JPanel panelRechts = new JPanel();
		getContentPane().add(panelRechts, BorderLayout.EAST);
		GridBagLayout gbl_panelRechts = new GridBagLayout();
		gbl_panelRechts.columnWidths = new int[]{105, 0};
		gbl_panelRechts.rowHeights = new int[]{23, 23, 23, 23, 0};
		gbl_panelRechts.columnWeights = new double[]{0.0, Double.MIN_VALUE};
		gbl_panelRechts.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panelRechts.setLayout(gbl_panelRechts);
		
		JButton buttonProtokollAnlegen = new JButton("Protokoll anlegen");
		buttonProtokollAnlegen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ProtokollAnlegenDialog protokollAnlegenDialog = new ProtokollAnlegenDialog(ProtokolleVerwaltenDialog.this, ProtokolleVerwaltenDialog.this.mainController, ProtokolleVerwaltenDialog.this.patientenId, -1);
				
				if (protokollAnlegenDialog.showDialog()) {
					tableProtokolleAktualisieren();
					tableProtokolle.setRowSelectionInterval(modelProtokolle.getRowCount() - 1, modelProtokolle.getRowCount() - 1);
				}
			}
		});
		GridBagConstraints gbc_buttonProtokollAnlegen = new GridBagConstraints();
		gbc_buttonProtokollAnlegen.fill = GridBagConstraints.HORIZONTAL;
		gbc_buttonProtokollAnlegen.anchor = GridBagConstraints.WEST;
		gbc_buttonProtokollAnlegen.insets = new Insets(0, 0, 5, 0);
		gbc_buttonProtokollAnlegen.gridx = 0;
		gbc_buttonProtokollAnlegen.gridy = 0;
		panelRechts.add(buttonProtokollAnlegen, gbc_buttonProtokollAnlegen);
		
		JButton buttonAbbrechen = new JButton("Abbrechen");
		buttonAbbrechen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				dispose();
			}
		});
		
		JButton buttonProtokollAendern = new JButton("Protokoll \u00E4ndern");
		buttonProtokollAendern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selectedRowIndex = tableProtokolle.getSelectedRow();

				if (selectedRowIndex >= 0) {
					int terminId = (int) modelProtokolle.getValueAt(selectedRowIndex, 0);
					ProtokollAnlegenDialog protokollAnlegenDialog = new ProtokollAnlegenDialog(ProtokolleVerwaltenDialog.this, ProtokolleVerwaltenDialog.this.mainController, ProtokolleVerwaltenDialog.this.patientenId, terminId);
					protokollAnlegenDialog.setVisible(true);
					
					tableProtokolleAktualisieren();
					tableProtokolle.setRowSelectionInterval(selectedRowIndex, selectedRowIndex);
				}
			}
		});
		GridBagConstraints gbc_buttonProtokollAendern = new GridBagConstraints();
		gbc_buttonProtokollAendern.insets = new Insets(0, 0, 5, 0);
		gbc_buttonProtokollAendern.fill = GridBagConstraints.HORIZONTAL;
		gbc_buttonProtokollAendern.gridx = 0;
		gbc_buttonProtokollAendern.gridy = 1;
		panelRechts.add(buttonProtokollAendern, gbc_buttonProtokollAendern);
		GridBagConstraints gbc_buttonAbbrechen = new GridBagConstraints();
		gbc_buttonAbbrechen.insets = new Insets(0, 0, 5, 0);
		gbc_buttonAbbrechen.fill = GridBagConstraints.HORIZONTAL;
		gbc_buttonAbbrechen.anchor = GridBagConstraints.WEST;
		gbc_buttonAbbrechen.gridx = 0;
		gbc_buttonAbbrechen.gridy = 2;
		panelRechts.add(buttonAbbrechen, gbc_buttonAbbrechen);

	}

	private void tableProtokolleAktualisieren() {
		modelProtokolle.setRowCount(0);
		List<Protokoll> protokolle = mainController.getPatientenController().getProtokolle(patientenId);
		
		for (int i = 0; i < protokolle.size(); i++) {
			Protokoll currentProtokoll = protokolle.get(i);
			Object[] rowData = {currentProtokoll.getTermin().getTerminId(), currentProtokoll.getTermin().getDatum(), currentProtokoll.getInhalt()};
			modelProtokolle.addRow(rowData);
		}
	}

}
